﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.ComponentModel;
using System.Drawing;

namespace Djs.Tools.FileCommander.Components
{
    public class FilePanel : Panel
    {
        public FilePanel()
        {
            this.Path = new FilePathPanel(this) { Dock = DockStyle.Top };
            this.Grid = new FileDataGrid(this) { Dock = DockStyle.Fill };
            this.Find = new FileFindPanel(this) { Dock = DockStyle.Bottom };

            ((System.ComponentModel.ISupportInitialize)(this.Grid)).BeginInit();

            this.Controls.Add(this.Grid);
            this.Controls.Add(this.Path);
            this.Controls.Add(this.Find);

            ((System.ComponentModel.ISupportInitialize)(this.Grid)).EndInit();

        }
        public FilePathPanel Path { get; private set; }
        public FileDataGrid Grid { get; private set; }
        public FileFindPanel Find { get; private set; }


        #region Data (FileGroup), Repaint, Refresh, Reload
        /// <summary>
        /// true když máme data
        /// </summary>
        public bool HasData { get { return (this._Data != null); } }
        /// <summary>
        /// Data o skupině souborů a adresářů
        /// </summary>
        public FileSystem.FileGroup Data {
            get { return this._Data; }
            set { this._Data = value; this._Reload(); }
        }
        public void Reload() { this._Reload(); }
        public void Repaint() { this._Repaint(); }
        private FileSystem.FileGroup _Data;

        private void _Reload()
        {
            if (!this.HasData) return;
            this.Path.Reload();
            this.Grid.Reload();
            this.Find.Reload();
        }
        private void _Repaint()
        {
            if (!this.HasData) return;
            this.Path.Repaint();
            this.Grid.Repaint();
            this.Find.Repaint();
        }
        
        #endregion
    }
    /// <summary>
    /// Horní řádek na FilePanelu = obsahuje cestu a vykresluje volný prostor na disku
    /// </summary>
    public class FilePathPanel : Panel
    {
        public FilePathPanel()
        {
            this.Height = 28;
        }
        #region Konstruktor, Owner, Data
        public FilePathPanel(FilePanel owner)
            : this()
        {
            this.Owner = owner;
        }
        /// <summary>
        /// Náš Owner = kompletní panel
        /// </summary>
        public FilePanel Owner { get; private set; }
        /// <summary>
        /// true když máme data (respektive, my máme Ownera, a náš Owner má data)
        /// </summary>
        public bool HasData { get { return (this.Owner != null && this.Owner.HasData); } }
        /// <summary>
        /// Data o skupině souborů a adresářů
        /// </summary>
        public FileSystem.FileGroup Data { get { return (this.HasData ? this.Owner.Data : null); } }
        #endregion
        #region Reload, Repaint
        internal void Reload()
        {
        }
        internal void Repaint()
        {
            this.Invalidate(true);
        }
        #endregion

    }
    /// <summary>
    /// Střední část na FilePanelu = Grid s daty
    /// </summary>
    public partial class FileDataGrid : DataGridView
    {
        #region Konstruktor, Owner, Data
        public FileDataGrid(FilePanel owner)
            : this()
        {
            this.Owner = owner;
        }
        /// <summary>
        /// Náš Owner = kompletní panel
        /// </summary>
        public FilePanel Owner { get; private set; }
        /// <summary>
        /// true když máme data (respektive, my máme Ownera, a náš Owner má data)
        /// </summary>
        public bool HasData { get { return (this.Owner != null && this.Owner.HasData); } }
        /// <summary>
        /// Data o skupině souborů a adresářů
        /// </summary>
        public FileSystem.FileGroup Data { get { return (this.HasData ? this.Owner.Data : null); } }
        #endregion
        #region Reload, Repaint
        internal void Reload()
        {
            if (!this.HasData) return;
            this.Rows.Clear();
            foreach (FileSystem.FileItem fileItem in this.Data.ItemList)
                this.Rows.Add(new FileGridRow(this, fileItem));
        }
        internal void Repaint()
        {
            this.Invalidate(true);
        }
        #endregion
    }
    /// <summary>
    /// Dolní řádek na FilePanelu = obsahuje summary nebo FileFilter textbox
    /// </summary>
    public class FileFindPanel : Panel
    {
        public FileFindPanel()
        {
            this.Height = 28;
        }
        #region Konstruktor, Owner, Data
        public FileFindPanel(FilePanel owner)
            : this()
        {
            this.Owner = owner;
        }
        /// <summary>
        /// Náš Owner = kompletní panel
        /// </summary>
        public FilePanel Owner { get; private set; }
        /// <summary>
        /// true když máme data (respektive, my máme Ownera, a náš Owner má data)
        /// </summary>
        public bool HasData { get { return (this.Owner != null && this.Owner.HasData); } }
        /// <summary>
        /// Data o skupině souborů a adresářů
        /// </summary>
        public FileSystem.FileGroup Data { get { return (this.HasData ? this.Owner.Data : null); } }
        #endregion
        #region Reload, Repaint
        internal void Reload()
        {
        }
        internal void Repaint()
        {
            this.Invalidate(true);
        }
        #endregion

    }
}
